/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package es.unavarra.iws.routerinstall.gui;

import es.unavarra.iws.routerinstall.engine.QueryManager;
import es.unavarra.iws.routerinstall.engine.utils.StringUtils;
import java.awt.Container;
import javax.swing.JOptionPane;

/**
 *
 * @author aqm
 */
public class Busqueda extends javax.swing.JPanel {
    
    QueryManager qm = null;
    static final String ERROR = "No se encuentran resultados...";

    /**
     * Creates new form Menu
     */
    public Busqueda(QueryManager qm) {
        this.qm = qm;
        initComponents();
        //jlError.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlError = new javax.swing.JLabel();
        jlIcono = new javax.swing.JLabel();
        jbHome = new javax.swing.JButton();
        jlTitulo = new javax.swing.JLabel();
        jlBarra = new javax.swing.JLabel();
        jbBuscar = new javax.swing.JButton();
        jtBusqueda = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jpContenido = new javax.swing.JPanel();
        jlBackground = new javax.swing.JLabel();

        setLayout(null);

        jlError.setForeground(new java.awt.Color(255, 0, 51));
        add(jlError);
        jlError.setBounds(20, 140, 320, 20);

        jlIcono.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/unavarra/iws/routerinstall/resources/img/question32.png"))); // NOI18N
        add(jlIcono);
        jlIcono.setBounds(35, 30, 40, 30);

        jbHome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/unavarra/iws/routerinstall/resources/img/home32.png"))); // NOI18N
        jbHome.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbHomeMouseClicked(evt);
            }
        });
        add(jbHome);
        jbHome.setBounds(420, 20, 50, 50);

        jlTitulo.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jlTitulo.setForeground(java.awt.Color.white);
        jlTitulo.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jlTitulo.setText("Búsqueda de conceptos");
        add(jlTitulo);
        jlTitulo.setBounds(100, 20, 300, 50);

        jlBarra.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/unavarra/iws/routerinstall/resources/img/w70.png"))); // NOI18N
        add(jlBarra);
        jlBarra.setBounds(0, 20, 500, 50);

        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/unavarra/iws/routerinstall/resources/img/search32.png"))); // NOI18N
        jbBuscar.setText("Buscar");
        jbBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbBuscarMouseClicked(evt);
            }
        });
        add(jbBuscar);
        jbBuscar.setBounds(360, 90, 110, 40);

        jtBusqueda.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jtBusqueda.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtBusquedaKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtBusquedaKeyTyped(evt);
            }
        });
        add(jtBusqueda);
        jtBusqueda.setBounds(20, 90, 330, 40);
        add(jSeparator1);
        jSeparator1.setBounds(-20, 170, 500, 10);

        jpContenido.setOpaque(false);
        add(jpContenido);
        jpContenido.setBounds(0, 180, 480, 460);

        jlBackground.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/unavarra/iws/routerinstall/resources/img/swr2.png"))); // NOI18N
        add(jlBackground);
        jlBackground.setBounds(0, 0, 480, 640);
    }// </editor-fold>//GEN-END:initComponents

    private void jbHomeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbHomeMouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
        Container menu = this.getFocusCycleRootAncestor();
        Menu principal = new Menu(qm);
        menu.remove(this);
        principal.setSize(menu.getSize());
        menu.add(principal, 0);        
    }//GEN-LAST:event_jbHomeMouseClicked

    private void jbBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbBuscarMouseClicked
        busquedaconcepto(this.jtBusqueda.getText());
    }//GEN-LAST:event_jbBuscarMouseClicked

    private void jtBusquedaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtBusquedaKeyTyped
        /*if(jlError.getText()!=null && jlError.getText().length()>0){
            jlError.setText("");
        }*/
    }//GEN-LAST:event_jtBusquedaKeyTyped

    private void jtBusquedaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtBusquedaKeyPressed
        if(evt.getKeyCode()==java.awt.event.KeyEvent.VK_ENTER ){
            busquedaconcepto(this.jtBusqueda.getText());
            //repaint();
        }
    }//GEN-LAST:event_jtBusquedaKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbHome;
    private javax.swing.JLabel jlBackground;
    private javax.swing.JLabel jlBarra;
    private javax.swing.JLabel jlError;
    private javax.swing.JLabel jlIcono;
    private javax.swing.JLabel jlTitulo;
    private javax.swing.JPanel jpContenido;
    private javax.swing.JTextField jtBusqueda;
    // End of variables declaration//GEN-END:variables

    private void busquedaconcepto(String sBusqueda) {
        String sResultado, sTitulo, sLogo, sComment, sURI;
        if(!sBusqueda.isEmpty()){
            jpContenido.removeAll();
            jlError.setText("");            
            sResultado = qm.executeQueryBasicConcepts(sBusqueda);
            if(sResultado!=null && sResultado.length()>0){
                    sComment = qm.getComment(sResultado);
                    sURI = qm.getSeeAlso(sResultado);
                    //sTitulo = sResultado;                           //cambiar por la funcion
                    sTitulo = qm.getTitle(sResultado);
                    //sLogo = "c_"+sResultado.toLowerCase() + ".png"; //cambiar por la funcion
                    sLogo = qm.getImage(sResultado);
                    jpContenido.add(new CConceptos((sTitulo!=null && sTitulo.length()>0)?sTitulo:sResultado, 
                            sLogo, sComment, 
                            (sURI!=null && sURI.length()>0) ? sURI : ("http://www.google.es/search?q=" + (sBusqueda.replaceAll(" ", "+")) + "+" + sResultado)));
                }
            else{
                this.jlError.setText(ERROR);
            }
            jpContenido.revalidate();
            jpContenido.repaint();
        }
    }
}
